package com.zjmzxfzhl.modules.app.controller;

import java.util.*;

import javax.validation.Valid;

import com.zjmzxfzhl.common.core.util.SecurityUtils;
import com.zjmzxfzhl.modules.app.entity.AppTag;
import com.zjmzxfzhl.modules.app.form.AppTaskVo;
import com.zjmzxfzhl.modules.sys.entity.vo.ElTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppTemplate;
import com.zjmzxfzhl.modules.app.service.AppTemplateService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/template")
public class AppTemplateController extends BaseController {
    @Autowired
    private AppTemplateService appTemplateService;

    /**
     * 自定义查询列表
     * 
     * @param appTemplate
     * @param current
     * @param size
     * @return
     */
    @PreAuthorize("@elp.single('app:template:list')")
    @GetMapping(value = "/list")
    public Result list(AppTemplate appTemplate, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<AppTemplate> pageList = appTemplateService.list(new Page<AppTemplate>(current, size), appTemplate);
        return Result.ok(pageList);
    }

    @PreAuthorize("@elp.single('app:template:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppTemplate appTemplate = appTemplateService.getById(id);
        return Result.ok(appTemplate);
    }

    /**
     * @功能：新增
     * @param appTemplate
     * @return
     */
//    @PreAuthorize("@elp.single('app:template:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppTemplate appTemplate) {
        Boolean result=appTemplateService.saveAppTemplate(appTemplate);
        if(result==false){
            return Result.error("保存失败！");
        }
        return Result.ok();
    }

    /**
     * @功能：修改
     * @param appTemplate
     * @return
     */
//    @PreAuthorize("@elp.single('app:template:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppTemplate appTemplate) {
        appTemplate.setParentTempId(null);
        appTemplate.setTempLevel(null);
        appTemplate.setTempLevelCode(null);
        appTemplate.setIsLeaf(null);
        Boolean result=appTemplateService.updateAppTemplate(appTemplate);
        if(result==false){
            return Result.error("修改失败！");
        }
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
//    @PreAuthorize("@elp.single('app:template:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        Boolean result=appTemplateService.delByIds(ids);
        if(result==false){
            return Result.error("检查目录下是否存在未删除子任务！");
        }
        return Result.ok();
    }

    /**
     *返回树结构
     * @return
     */
//    @PreAuthorize("@elp.single('app:task:list')")
    @GetMapping(value = "/getTreeData")
    public Result getSelectTreeData(AppTemplate appTemplate) {

        List<ElTree> treeData = appTemplateService.getTreeData(appTemplate);

        return Result.ok(treeData);
    }
}
